//import Request from '@/utils/request'
import {getStorage} from '@/utils/common.js'
const state = () => ({
	userName: 'zty',
	userInfo:{},//JSON.parse(getStorage('userInfo') || '{}'),
	goods:[], // 购买商品列表
	orderInfo:{ // 订单信息
		selfServiceFlag:1, //是否自提/堂食。1.是；2.否
		orderType:1, // 订单类型：1为商城类订单，2位服务类订单
		serviceType:'', // 服务类型：1为助餐，2为助医，3为助浴，4为助洁，5为助急
		deliveryFee:0  // 配送费，默认为0
	} 
})
const getters = {
	userName(state) {
		return state.userName;
	},
	goods(state) {
		return state.goods;
	},
	orderInfo(state) {
		return state.orderInfo;
	},
	userInfo(state) {
		return state.userInfo;
	},
}
const mutations = {
	set_userName: (state, data) => {
		state.userName = data
	},
	add_goods: (state, data) => {
		let goodsItem = ''

		// 查找是否已经存在了该商品
		const item = state.goods.find(ele => ele.id == data.id)
		if(item){
			item.quantity ++
		}else{
			data.quantity = 1 // 默认购买数据为1
			goodsItem = data
			state.goods.push(goodsItem)
		}
		
	},
	clear_goods: (state, data) => {
		state.goods = []
	},
	del_goods: (state, goodsId) => {
		if(goodsId){ // 删除指定id的数据
			const index = state.goods.findIndex(item =>item.id == goodsId)
			index && state.goods.splice(index,1)
		}else{  // 删除quantity=0的数据
			let tmps = []
			state.goods.forEach(item =>{
				if(item.quantity>0){
					tmps.push(item)
				}
			})
			state.goods = tmps
		}
	},
	set_orderInfo: (state, obj) => {
		 Object.assign(state.orderInfo,obj)
	},
	set_userInfo: (state, obj) => {
		 Object.assign(state.userInfo,obj)
	},
}
const actions = {
	// async login({commit}, params) { 
	// 	return new Promise((resolve,reject)=>{
	// 		Request.post('/mobile/user/login',params)
	// 		.then(res=>{
	// 			resolve(res.data) 
	// 		})
	// 	})
	// },
}

export default {
	namespaced: true,
	state,
	getters,
	mutations,
	actions
}
